package com.huizluo.module4.chatroom;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class ClientTest2 {
    public static void main(String[] args) throws IOException {

        Scanner scanner = new Scanner(System.in);
        Client client = new Client("127.0.0.1",8888);
        client.connect();
        client.recv();

        while (true) {
            String in = scanner.nextLine();
            String[] ins = in.split(" ");

            if (ins.length != 2) {
                System.out.println("输入有误");
                continue;
            }
            int t = Integer.parseInt(ins[0]);
            
            if (t != 0 && t != 1) {
                System.out.println("输入有误");
                continue;
            }

            Message msg;

            if (t == 0) {
                msg = new Message(t, ins[1].getBytes());
                try {
                    client.send(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } else {
                File file = new File(ins[1]);
                if (!file.isFile()){
                    System.out.println("文件不存在");
                    continue;
                }
                //先通知服务端接收文件
                System.out.println("通知服务端接收文件");
                msg = new Message(t,"传输文件".getBytes());
                client.send(msg);
                client.sendFile(file);
            }
        }
    }
}
